import React, { useState, useEffect } from 'react';
import { for_obj, d_sort, obj_str, loc_dt, obj_int, useHigh, GridTable, loc_str, FindBarBase, useCmd, WaitUmd, MainView, cline, loc, uJob } from 'sui';
import { get_init } from 'trace/cmd';
import { utc_offset } from 'cache';

const WorkView = () => {
    const [sum, setSum] = useState([]);
    const [main, setMain] = useState();
    const [opt_find, optFind] = useState();
    const umd = useCmd(null, "trace");
    const [high] = useHigh(170, 100);

    useEffect(() => get_init(optFind), []);

    const hfind = (value) => {
        if (!value["time"]) {
            alert(loc("l_307"));
            return;
        }

        const utc = utc_offset();

        const s = loc_dt(value["t_start"]);
        const e = loc_dt(value["t_end"]);

        const opt = [
            {ge: "time", v: loc_str(value["t_start"]), utc: utc},
            {le: "time", v: loc_str(value["t_end"]), utc: utc},
            {eq: "line", v: cline()},
        ];

        umd.startWait();

        uJob(umd.endWait, "work/census",
            {
                line: cline(),
                s: s,
                e: e,
                opt: opt,
            },
            r => {
                const auto = [];
                const hand = [];
                const map_sum = {};

                r.forEach(d => {
                    const tsn = obj_str(d, "tech");

                    if (d.tp === "hand") {
                        const test = Math.min(obj_int(d, "standout"), obj_int(d, "angle"), obj_int(d, "rotate"));

                        d["inner"] = Math.min(obj_int(d, "india"), obj_int(d, "indt"), obj_int(d, "inwidth"));
                        d["outer"] = Math.min(obj_int(d, "outdia"), obj_int(d, "outdt"), obj_int(d, "outwidth"));
                        d["test"] = test;

                        if (test > 0) {
                            if (map_sum[tsn]) {
                                map_sum[tsn] += test;
                            } else {
                                map_sum[tsn] = test;
                            }
                        }

                        hand.push(d);
                    } else if (d.tp === "auto") {
                        const inner = obj_int(d, "autoin");
                        const outer = obj_int(d, "autout");
                        const test = Math.min(obj_int(d, "standout"), obj_int(d, "angle"), obj_int(d, "kia"));

                        d["inner"] = inner;
                        d["india"] = inner;
                        d["indt"] = inner;
                        d["inwidth"] = inner;

                        d["outer"] = outer;
                        d["outdia"] = outer;
                        d["outdt"] = outer;
                        d["outwidth"] = outer;

                        d["rotate"] = obj_int(d, "kia");
                        d["pairlog"] = obj_int(d, "apairlog");
                        d["test"] = test;

                        if (test > 0) {
                            if (map_sum[tsn]) {
                                map_sum[tsn] += test;
                            } else {
                                map_sum[tsn] = test;
                            }
                        }

                        auto.push(d);
                    }
                });

                d_sort(auto, "tech");
                d_sort(hand, "tech");

                const ret = [];

                hand.forEach(d => ret.push(d));
                auto.forEach(d => ret.push(d));

                ret.forEach((d, i) => d["index"] = i + 1);

                const ret_sum = [{ index: loc("l_363"), tp: loc("l_842") }];
                for_obj(map_sum, (k, v) => ret_sum.push({ index: k, tp: v }))

                setSum(ret_sum);

                setMain(ret);
            },
            e => setMain(null));
    };

    const col = [
        {name: loc("l_193"), key: 'index', width: 110, frozen: true, summaryFormatter: ({ row }) => <strong>{obj_str(row, "index")}</strong>},
        {name: loc("l_229"), key: 'tp', width: 150, frozen: true, formatter: (props) => {
            const d = props.row;
            const t = obj_str(d, "tp");

            if (t === "hand") {
                return loc("l_446");
            } else if (t === "auto") {
                return loc("l_756");
            }

            return t;
        }, summaryFormatter: ({ row }) => <strong>{obj_str(row, "tp")}</strong>},
        {name: loc("l_363"), key: 'tech', width: 110, frozen: true},
        {name: `${loc("l_784")}${loc("l_807")}`, key: 'india', width: 110},
        {name: `${loc("l_784")}${loc("l_806")}`, key: 'indt', width: 110},
        {name: `${loc("l_784")}${loc("l_805")}`, key: 'inwidth', width: 110},
        {name: `${loc("l_783")}${loc("l_807")}`, key: 'outdia', width: 110},
        {name: `${loc("l_783")}${loc("l_806")}`, key: 'outdt', width: 110},
        {name: `${loc("l_783")}${loc("l_805")}`, key: 'outwidth', width: 110},
        {name: loc("l_838"), key: 'inner', width: 110},
        {name: loc("l_839"), key: 'outer', width: 110},
        {name: loc("l_840"), key: 'pairlog', width: 110},
        {name: loc("l_841"), key: 'packlog', width: 110},
        {name: loc("l_748"), key: 'standout', width: 110},
        {name: loc("l_843"), key: 'angle', width: 110},
        {name: loc("l_844"), key: 'rotate', width: 110},
        {name: loc("l_842"), key: 'test', width: 146},
    ];

    const hclear = () => get_init(optFind);

    const ctl = {
        ctl: [
            {sn: "time", name: loc("l_308"), ctl: "IkFindDate", start: "t_start", end: "t_end"},
        ]
    };

    return (
        <MainView>
            <FindBarBase left={loc("l_837")} hcmd={hfind} data={ctl} hclear={hclear} cur={opt_find}/>
            <GridTable col={col} row={main} high={high} sumrow={sum}/>
            <WaitUmd umd={umd}/>
        </MainView>
    );
};

export {WorkView};
